<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {select_cascading_checkbox} function plugin
 *
 * Type:     function<br>
 * Name:     select_cascading_checkbox<br>
 * Date:     Sept 25, 2012<br>
 * Examples:
 * <pre>
 * {select_cascading_checkbox list=$data}
 * </pre>
 * @author   Toan Vu <toan@dirox.net>
 * @version  1.0
 * @param array
 * @param Smarty
 * @return string
 */
function smarty_function_select_cascading_checkbox($params, &$smarty)
{    
    /* Default values. */
    $itemperline    = 4;
    
    if (!isset($params['list'])) {
        $smarty->trigger_error("select_cascading_checkbox: missing 'list' parameter");
        return;
    }
    if (!isset($params['name'])) {
        $smarty->trigger_error("select_cascading_checkbox: missing 'name' parameter");
        return;
    }
    
    if (isset($params['itemperline'])) {
        $itemperline = $params['itemperline'];
    }

    $data = array();
    foreach ($params['list'] as $key => $value) {
        if(!isset($value['id']) || !isset($value['value']) || !isset($value['list'])) {
            continue;
        }
        $id_list = (int)$value['id'];
        $id_item = (int)$value['list']['id'];
        $data[$id_list]['NAME'] = (string)$value['value'];
        $data[$id_list]['LIST'][$id_item] = (string)$value['list']['value'];
    }
    
    $select  = '<select style="margin-bottom:10px" id="'.$params['name'].'_main" name="'.$params['name'].'_main" onchange="updateCascading(\''.htmlentities($params['name']).'\')">';
    $select .= '<option value=""></option>';
    foreach($data as $key => $value) {        
        $select .= '<option value="'.$key.'">'.StringUtil::escapeUTF8ToHTML($value['NAME']).'</option>';
    }
    $select .= '</select>';
    $checkbox = '';    
    foreach($data as $key => $value) {
        if(!isset($value['LIST']) || count($value['LIST'])==0) {
            continue;
        }
        $checkbox .= '<div style="line-height:20px; /*display:none*/" id="'.$params['name'].'_cascade_'.$key.'" class="'.$params['name'].'_cascade_class">';
        $checkbox .= '<table cellspacing="0" cellpadding="0" border="0">';
        $count = 0;
        foreach($value['LIST'] as $key1 => $value1) {
            if($count==0) {
                $checkbox .= '<tr>';
            }            
            $count++;
            $checkbox .= '<td nowrap="nowrap"><input type="checkbox" value="'.$key1.'" id="'.$params['name'].'_'.$key1.'" name="'.$params['name'].'[]" />'.$value1.'&nbsp;&nbsp;&nbsp;</td>';
            if($count==$itemperline) {                
                $checkbox .= '<td width="100%"></td></tr>';
                $count = 0;
            }
        }
        if($count < $itemperline && $count > 0) {
            $checkbox .= '<td colspan="'.($itemperline-$count).'"></td><td width="100%"></td></tr>';
        }
        $checkbox .= '</table>';
        $checkbox .= '</div>';
    }
    $script = '
    <script>
    $(function() {
        $("div [class=\''.$params['name'].'_cascade_class\']").hide();        
        $("div [id=\'"+"'.$params['name'].'"+"_cascade_" + $("select[id=\''.$params['name'].'_main\']").val()+"\']").show();       
    });    
    if(typeof updateCascading !== "function"){
        function updateCascading(name){
            $("div [class=\'" + name + "_cascade_class\']").each(function(){
                $("input:checkbox",$(this)).each(function(){$(this).attr("checked",false)});
            }).hide();        
            var cascading_show_id = name +"_cascade_" + $("select[id=\'" + name + "_main\']").val();
            $("div [id=\'"+ cascading_show_id +"\']").show();             
        }
    }
    </script>';
    
    
    return $select.$checkbox.$script;
}

/* vim: set expandtab: */